<?php

/***************************************************************\
* FILE:		sys/pages/dir.inc
* SECTION:	pages > core set
* FUNCTION:	give an overview of the contents of a directory
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/forumfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
}

/**
 *
 */
function page_inc_execute_action($action) {
	global $PAGE;
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];

	switch($action) {
		case "markread":
			if (!$_SESSION['user_id'])
				errorpage(6);
			$from = array("ITEM_IN INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id", "DUMMY");
			$where = "SUB_IN.dir_id = {$THISDIR['dir_id']}";
			$sql = db_query_select(array(array(), "ITEM_IN.item_id"), $from, $where);
			$sqlresult = db_do_query($sql);
			$values = array();
			while ($r = db_fetch_array($sqlresult))
				array_push($values, array($r['item_id'], $_SESSION['user_id']));
			if (count($values)) {
				$sql = db_query_insert_multiple(array(item_id, user_id), $values, "HAS_READ", 1);
				db_do_query($sql);
				$n = db_affected_rows();
				$PAGE['menu'][$THISDIR['dir_id']]['new_items'] = 0;
				$PAGE['menu'][$THISDIR['dir_id']]['read_items'] += $n;
			}
			add_alert("alles gelezen hier...");
			break;
		case "ignore":
			if (!$_SESSION['user_id'])
				errorpage(6);
			$sql = db_query_insert(array('dir_id', 'user_id'), array($THISDIR['dir_id'], $_SESSION['user_id']), "IGNORE_DIR");
			db_do_query($sql);
			add_alert("directory '{$THISDIR['title']}' wordt vanaf nu genegeerd");
			$PAGE['menu'][$THISDIR['dir_id']]['ignored'] = 1;
			break;
		case "unignore":
			if (!$_SESSION['user_id'])
				errorpage(6);
			$sql = db_query_delete("IGNORE_DIR", "dir_id = {$THISDIR['dir_id']} AND user_id = {$_SESSION['user_id']}");
			db_do_query($sql);
			add_alert("directory '{$THISDIR['title']}' wordt vanaf nu niet meer genegeerd");
			$PAGE['menu'][$THISDIR['dir_id']]['ignored'] = 0;
			break;
		default:
			add_alert("unknown action: $action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {

	// backwards compatible
	if ($_GET['subber'])
		redirect("{$PAGE['dirpath']}/{$_GET['subber']}.subset");
	
	global $PAGE, $CONFIG;
	$CONTENT = array();
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];
	
	$CONTENT['title'] = $THISDIR['title']; 

	if ($THISDIR['show_items']) {

		$CONTENT['items'] = array();
	
		$where = "ITEM.item_type_id = ITEM_TYPE.item_type_id AND \n\t";
		$where .= "ITEM.item_id = ITEM_IN.item_id AND \n\t";
		$where .= "ITEM_IN.sub_id = SUB_IN.sub_id AND \n\t";
		$where .= "SUB_IN.dir_id = {$THISDIR['dir_id']} AND \n\t";
		$where .= "ITEM.author = M1.user_id AND \n\t";
		$where .= "ITEM.editor = M2.user_id AND \n\t";
		$where .= "NOT ITEM_IN.deleted AND \n\t";
		$where .= "NOT ITEM.deleted";

		$order = "sticky DESC, " . $THISDIR['sorting_sql'];
		if ($THISDIR['show_subsets']) 
			$order = "SUB_IN.sub_title ASC, $order";

		$sql = db_query_select(array(array("*"), array("*"), array(), array("*"), array(), array(), "M1.nick as nick_author, M2.nick as nick_editor"), array("ITEM", "ITEM_TYPE", "ITEM_IN", "SUB_IN", "MEMBER AS M1", "MEMBER AS M2", "DUMMY"), $where, $order, $limit);

		$items = db_do_query($sql);
		
		$read = array();

		if ($_SESSION['user_id']) {
			$readings = db_do_query(db_query_select(array(array(), "HAS_READ.item_id"), array("(((HAS_READ INNER JOIN ITEM ON HAS_READ.item_id = ITEM.item_id) INNER JOIN ITEM_IN ON HAS_READ.item_id = ITEM_IN.item_id) INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id)" . ($THISDIR['show_subsets'] ? " INNER JOIN SUBSET ON SUB_IN.sub_id = SUBSET.sub_id" : "")), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND user_id = {$_SESSION['user_id']}", $order, $limit));
			while ($item = db_fetch_array($readings))
				$read[$item['item_id']] = 1;
		}

		if ($THISDIR['show_subsets'])
			$CONTENT['subsets']['visible'] = array();

		while ($item = db_fetch_array($items)) {
			if ($THISDIR['show_subsets'] and ($item['sub_id'] != $prev_sub))
				array_unshift($CONTENT['subsets']['visible'], array('sub_id' => $item['sub_id'], 'sub_title' => $item['sub_title']));
			$item['unread'] = $_SESSION['user_id'] ? !$read[$item['item_id']] : 0;
			$item['new'] = (($item['edit'] > $_SESSION['member']['last_visit']) and $item['unread']) ? 1 : 0;
			array_unshift($CONTENT['items'], $item);
			$prev_sub = $item['sub_id'];
		}
		
		if ($THISDIR['show_subsets']) {
			$CONTENT['subsets']['all'] = array();
			if ($_SESSION['user_id']) {
				$readresult = db_do_query(db_query_select(array(array(), "SUB_IN.sub_id, COUNT(HAS_READ.item_id) AS `reads`"),
									  array("(SUB_IN INNER JOIN ITEM_IN ON SUB_IN.sub_id = ITEM_IN.sub_id) INNER JOIN HAS_READ ON ITEM_IN.item_id = HAS_READ.item_id"), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND user_id = {$_SESSION['user_id']} AND !ITEM_IN.deleted", "SUB_IN.sub_id ASC", "", "sub_id"));
				$reads = array();
				while ($r = db_fetch_array($readresult))
					$reads[$r['sub_id']] = $r['reads'];
			}
			$sql = db_query_select(array(array(), 'SUBSET.body_id, SUB_IN.*, COUNT(ITEM_IN.item_id) AS items, 0 AS `reads`'), 
						array("(SUBSET INNER JOIN SUB_IN ON SUBSET.sub_id = SUB_IN.sub_id) LEFT JOIN ITEM_IN ON SUB_IN.sub_id = ITEM_IN.sub_id"), 
						"SUB_IN.dir_id = {$THISDIR['dir_id']} AND !ITEM_IN.deleted", "SUB_IN.sub_title ASC", "", "sub_id");
			$sqlresult = db_do_query($sql);
			while ($s = db_fetch_array($sqlresult)) {
				if ($_SESSION['user_id'])
					$s['reads'] = $reads[$s['sub_id']];
				if (has_access('MODERATE') or has_access('ADMIN'))
					$s['editlink'] = "{$PAGE['dirpath']}/{$s['sub_id']}.edit_sub";
				if ($s['items'])
					array_unshift($CONTENT['subsets']['all'], $s);
			}
		}
		
		add_action('show-all-subs', 'javascript', '', 'show_all_subs();');
	}
		
	$PAGE['content'] = $CONTENT;

	if ($_SESSION['user_id']) {
		
		if ($THISDIR['show_items']) // mark all items read
			add_action('markread', 'url-action', 'markread', '', 0);
		if ($PAGE['menu'][$THISDIR['dir_id']]['children']) // mark all items read
			add_action('markread-sub', 'url', "{$THISDIR['dir_id']}.fora?action=markread-sub");
	
		// ignoring
		if (!$PAGE['menu'][$THISDIR['dir_id']]['ignored'])
			add_action('ignore', 'url-action', 'ignore');
		else
			add_action('unignore', 'url-action', 'unignore');
			
		// subscribing
		if (!$PAGE['menu'][$THISDIR['dir_id']]['subscription'])
			add_action('subscribe_dir', 'popup', "{$PAGE['dirpath']}/.subscribe");
		else
			add_action('altersubscription_dir', 'popup', "{$PAGE['dirpath']}/.subscribe");
			
		// adding
		if (has_any_add_access() and $THISDIR['show_items'])
			add_action('add_item', 'url', '.add_item', '', 1);
			
		// edit directory settings
		if (has_access("MODERATE") or has_access("ADMIN"))
			add_action('edit_dir', 'url', '.edit_dir');
	}
	
	$sql = db_query_update(array('hits'), array('hits + 1'), "DIRECTORY", "dir_id = {$THISDIR['dir_id']}");
	db_do_query($sql);

	if ($THISDIR['show_subfora'] and is_array($PAGE['menu'][$THISDIR['dir_id']]['children']) and count($PAGE['menu'][$THISDIR['dir_id']]['children']))
		$PAGE['subfora'] = fora_get_subfora($THISDIR['dir_id'], $PAGE['menu']);
	
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;

	fora_page_inc_check_occurences();
	if($PAGE['occurences']) {
		print_occurences($PAGE['occurences']);
	}
	if ($PAGE['directory_tree'][$PAGE['dir_depth']-1]['show_body'])
		print_body($PAGE['body_main']);
	if ($PAGE['directory_tree'][$PAGE['dir_depth']-1]['show_subsets'])
		print_subsetlist($PAGE['content']['subsets']['all'], $PAGE['content']['subsets']['visible'], $PAGE['dirpath'], $_SESSION['user_id'] ? 1 : 0);

	if ($PAGE['subfora'])
		print_subfora($PAGE['subfora'], $PAGE['dirpath']);
	
	if ($PAGE['directory_tree'][$PAGE['dir_depth']-1]['show_items'])
		print_itemlist($PAGE['content']['items'], $PAGE['dirpath'], $PAGE['directory_tree'][$PAGE['dir_depth']-1]['crop_topiclist']);

	if($PAGE['occurences']) {
		print_occurences($PAGE['occurences']);
	}
}

?>
